#include<bits/stdc++.h>
using namespace std;

int T, n, m,a[200010], b[200010], c[200010], C[200010], K;
bool g[21][200010];

inline void solve();
inline int calc(int K);

inline void read(int& n){
    n = 0;
    int f = 1;
    char c;
    for(c = getchar(); c > '9' || c < '0'; c = getchar())
        if(c == '-')
            f = -1;
    for(; c >= '0' && c <= '9'; c = getchar())
        n = n * 10 + c - '0';
    n *= f;
}

signed main(){
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    read(n), read(m);
    for(int i = 1; i <= n; i++)
        read(b[i]);
    for(int i = 1; i <= m; i++)
        read(C[i]);
    for(K = 0; (1 << K) <= n; K++);
    for(int i = 1; i <= K; i++)
        for(int j = 0, l = (1 << (K - i)); j <= l; j++)
            g[i][j] = getchar() - '0';
    read(T);
    for(; T--;)
        solve();
}

int X[4];

inline void solve(){
    for(int i = 0; i < 4; i++)
        read(X[i]);
    for(int i = 1; i <= n; i++)
        a[i] = b[i] ^ X[i % 4];
    int ans = 0;
    for(int w = 1; w <= m; w++){
        for(K = 0; (1 << K) <= c[w]; K++);
        ans ^= calc(K) * w;
    }
    printf("%d\n", ans);
}

inline int calc(int K){
    for(int i = 1; i <= m; i++)
        c[i] = C[i];
    for(int w = 1; w <= K; w++){
        for(int i = 1; i <= n; i += (1 << w)){
            if(g[w][(i + 1) / 2]){
                if((c[i] > c[i + (1 << (w - 1))] && a[c[i]] < w) || (c[i] < c[i + (1 << (w - 1))] && a[c[i + (1 << (w - 1))]] > w))
                    c[i] = c[i + (1 << (w - 1))];
            } else{
                if((c[i] < c[i + (1 << (w - 1))] && a[c[i]] < w) || (c[i] > c[i + (1 << (w - 1))] && a[c[i + (1 << (w - 1))]] > w))
                    c[i] = c[i + (1 << (w - 1))];
            }
        }
    }
    return c[1];
}
